#ifndef DEBRIDADTORRENT_H
#define DEBRIDADTORRENT_H

#include <list>
#include <cstring>

#include <config.h>

#include <debridDownloader.h>
#include <debridFetcherAD.h>

//FIXME
class debridFetcherAD;

class debridADTorrent : public debridDownloader {
    public:
        debridADTorrent(CURL *copyHandle, CURLSH *cookieShare, debridFetcherAD *newParent=NULL);
        virtual ~debridADTorrent();
        void attachParent(debridFetcherAD *newParent);
        bool addTorrent(const std::string& path);
        bool addMagnet(const std::string& magnet);
        void setExit(bool newState = true);
        bool getExit();
        void setRunning(bool newState = true);
        bool isFirstRun();
        /*
         * Thread functions
         */
        /**
        * \brief Start the thread.
        * \return True if the thread is successfuly created, false if it failed.
        */
        bool start();
        /**
        * \brief Cancel the current download.
        */
        bool cancel();
        /**
        * \brief Stop and delete the thread.
        * \param forceExit if true the current download will be cancelled too.
        */
        void stop(bool forceExit=true);
    protected:
        pthread_t thread;
        pthread_mutex_t dataLock;
        debridFetcherAD *parent;
        bool firstRun;
        bool mustExit;
        bool threadRunning;
        std::list<std::string> torrentList;
        void appendTorrent(const std::string& torrentLink);
        void processList();
        bool processTorrent(const std::string& torrent);
        bool pollTorrentStatus(bool isResuming=false);
        /*
         * Thread functions
         */
        bool createThread();
        static void* threadEntry(void *data);
        bool deleteThread();
};

#endif // DEBRIDADTORRENT_H
